/**
 * PlayableChar
 * @author Fede Ferran Sergio Rodriguez Bernal
 * Descripció:
 *
 *
 */
package entities;

import java.sql.ResultSet;
import java.sql.SQLException;
import org.newdawn.slick.Animation;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import physics.Physics;

/**
 *
 * @author wida52629504
 */
public class PlayableChar extends GameCharacter {

    // Declaracio de variables propies
    private boolean paredSaltable;
    private boolean doblesaltable;
    private boolean rompePared;
    private boolean transformable;
    private int energy;
    private int maxEnergy;

    // constructor que rep els parametres de la base de dades dels personatjes jugables
    public PlayableChar(ResultSet iniValues, ResultSet animations) {
        setAnimation(new Animation());
        setAnimationR(new Animation());
        setAnimationL(new Animation());
        setAnimationJumpR(new Animation());
        setAnimationJumpL(new Animation());
        setAnimationRR(new Animation());
        setAnimationRL(new Animation());
        setAnimationAtR(new Animation());
        setAnimationAtL(new Animation());
        setAnimationShtR(new Animation());
        setAnimationShtL(new Animation());
        setAnimationDownR(new Animation());
        setAnimationDownL(new Animation());
        setAnimationDownShtL(new Animation());
        setAnimationDownShtR(new Animation());
        setAnimationShtL(new Animation());
        setAnimationParedR(new Animation());
        setAnimationParedL(new Animation());
        setAnimationFastR(new Animation());
        setAnimationFastL(new Animation());
        setAnimationDieR(new Animation());
        setAnimationDieL(new Animation());
        setAnimationHurtR(new Animation());
        setAnimationHurtL(new Animation());
        try {
            while (iniValues.next()) {
                //asignació de les variables inicials a partir de les consultes
                setName(iniValues.getString(1));
                setMaxLife(iniValues.getInt(2));
                setLife(getMaxLife());
                energy = iniValues.getInt(3);
                maxEnergy = energy;
                setJump(iniValues.getFloat(4));
                setSpeed(iniValues.getFloat(5));
                setxLeft(iniValues.getInt(6));
                setxRight(iniValues.getInt(7));
                setyUp(iniValues.getInt(8));
                setyBottom(iniValues.getInt(9));
                setPoly(getxLeft(), getxRight(), getyUp(), getyBottom());
                setAxLeft(iniValues.getInt(10));
                setAxRight(iniValues.getInt(11));
                setAyUp(iniValues.getInt(12));
                setAyBottom(iniValues.getInt(13));
                setAtackPoly(getAxLeft(), getAxRight(), getAyUp(), getAyBottom());
                this.maxAtackTime = iniValues.getInt(14);
                this.maxShootTime = iniValues.getInt(15);
                setMargenUp(iniValues.getInt(16));
                setMargenDown(iniValues.getInt(17));
                paredSaltable = iniValues.getBoolean(18);
                doblesaltable = iniValues.getBoolean(19);
                rompePared = iniValues.getBoolean(20);
                transformable = iniValues.getBoolean(21);

                this.atackTime = 0;
                this.shootTime = 0;
                setLeft(false);
                setSaltable(true);
                setAtack(false);
                setShoot(false);
            }
            try {

                while (animations.next()) {
                    //asignació de les animacions a partir de les consultes
                    String accion = animations.getString("accion");
                    Image img = new Image(animations.getString("img_path"));
                    int frameTime = animations.getInt("frameTime");

                    if (accion.equals("jumpL")) {
                        getAnimationJumpL().addFrame(img, frameTime);
                    } else if (accion.equals("jumpR")) {
                        getAnimationJumpR().addFrame(img, frameTime);
                    } else if (accion.equals("standR")) {
                        getAnimationR().addFrame(img, frameTime);
                    } else if (accion.equals("standL")) {
                        getAnimationL().addFrame(img, frameTime);
                    } else if (accion.equals("moveR")) {
                        getAnimationRR().addFrame(img, frameTime);
                    } else if (accion.equals("moveL")) {
                        getAnimationRL().addFrame(img, frameTime);
                    } else if (accion.equals("cxcR")) {
                        getAnimationAtR().addFrame(img, frameTime);
                    } else if (accion.equals("cxcL")) {
                        getAnimationAtL().addFrame(img, frameTime);
                    } else if (accion.equals("shL")) {
                        getAnimationShtL().addFrame(img, frameTime);
                    } else if (accion.equals("shR")) {
                        getAnimationShtR().addFrame(img, frameTime);
                    } else if (accion.equals("runR")) {
                        getAnimationFastR().addFrame(img, frameTime);
                    } else if (accion.equals("runL")) {
                        getAnimationFastL().addFrame(img, frameTime);
                    } else if (accion.equals("paredR")) {
                        getAnimationParedR().addFrame(img, frameTime);
                    } else if (accion.equals("paredL")) {
                        getAnimationParedL().addFrame(img, frameTime);
                    } else if (accion.equals("dieR")) {
                        getAnimationDieR().addFrame(img, frameTime);
                        getAnimationDieR().setLooping(false);
                    } else if (accion.equals("dieL")) {
                        getAnimationDieL().addFrame(img, frameTime);
                        getAnimationDieL().setLooping(false);
                    } else if (accion.equals("hurtR")) {
                        getAnimationHurtR().addFrame(img, frameTime);
                    } else if (accion.equals("hurtL")) {
                        getAnimationHurtL().addFrame(img, frameTime);
                    } else if (accion.equals("downR")) {
                        getAnimationDownR().addFrame(img, frameTime);
                    } else if (accion.equals("downL")) {
                        getAnimationDownL().addFrame(img, frameTime);
                    } else if (accion.equals("downShR")) {
                        getAnimationDownShtR().addFrame(img, frameTime);
                    } else if (accion.equals("downShL")) {
                        getAnimationDownShtL().addFrame(img, frameTime);
                    }
                }
                //asignacio de la animació per defecte 
                setAnimation(getAnimationR());

            } catch (SlickException e) {
                System.out.println(e);
            }
        } catch (SQLException se) {
            System.out.println(se);
        }
    }

    //Getters i Setters
    public boolean isRompePared() {
        return rompePared;
    }

    public void setRompePared(boolean rompePared) {
        this.rompePared = rompePared;
    }

    public boolean isTransformable() {
        return transformable;
    }

    public void setTransformable(boolean transformable) {
        this.transformable = transformable;
    }

    public boolean isDoblesaltable() {
        return doblesaltable;
    }

    public void setDoblesaltable(boolean doblesaltable) {
        this.doblesaltable = doblesaltable;
    }

    public boolean isParedSaltable() {
        return paredSaltable;
    }

    public void setParedSaltable(boolean paredSaltable) {
        this.paredSaltable = paredSaltable;
    }

    public int getEnergy() {
        return energy;
    }

    public void setEnergy(int energy) {
        this.energy = energy;
    }

    public int getMaxEnergy() {
        return maxEnergy;
    }

    public void setMaxEnergy(int maxEnergy) {
        this.maxEnergy = maxEnergy;
    }
    //finalització de getters i setters

    //contructor proves
    public PlayableChar() {
        super();
    }

    //trucada al metode de la clase mare per tal de executar les funcions necesaries segons les accions
    @Override
    public void Update(GameContainer gc, int delta, Physics phys) {
        super.Update(gc, delta, phys);
    }
}
